<template>
    <div class="content">
        <el-dialog title="工废/料废信息" :visible.sync="dialogFormVisible" width="40%" :close-on-click-modal="false" :before-close="dilogFormBeforeClose" :modal-append-to-body="false">
            <el-form :model="formData" size="mini" :disabled="scrapInfoDisabled">
                <el-form-item :label="e.label" label-width="80px" v-for="e,i in reasonList" :key="i">
                    <el-input v-model="formData[e.bindDataName].downtimeDuration" autocomplete="off" @input="validateInput(e.bindDataName)" placeholder="请输入停机时长(H)"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="clearBtnEvent" size="mini" :disabled="scrapInfoDisabled">清空</el-button>
                <el-button type="primary" @click="confirmBtnEvent" size="mini" :disabled="scrapInfoDisabled">确定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
export default {
    name:"downtReason",
    props:{
        type:{
            type:String,
            require:false,
            default:''
        },
        reasonDataList:{
            type:Array,
            require:false,
            default:() => new Array()
        },
        scrapInfoDisabled:{
            type:Boolean,
            require:false,
            default:false
        }
    },
    data(){
        return {
            //机加工 工废 料废
            jjg_waste_gf:[
                { name:'换型',type:'',value:'' },
                { name:'换刀',type:'',value:'' },
                { name:'崩刀',type:'',value:'' },
                { name:'断刀',type:'',value:'' },
                { name:'过切',type:'',value:'' },
                { name:'装夹不良',type:'',value:'' },
                { name:'落地件',type:'',value:'' },
                { name:'工废',type:'',value:'' },
                { name:'大小边',type:'',value:'' },
                { name:'其它',type:'',value:'' }
            ],
            jjg_waste_lf:[],
            //走心机 工废 料废
            zxj_waste_gf:[
                { name:'换刀',type:'',value:'' },
                { name:'换型',type:'',value:'' },
                { name:'冷热机',type:'',value:'' },
                { name:'尺寸',type:'',value:'' },
                { name:'铁屑划伤',type:'',value:'' },
                { name:'来料不良',type:'',value:'' },
                { name:'工程打样',type:'',value:'' },
                { name:'其它',type:'',value:'' }
            ],
            zxj_waste_lf:[
                { name:'来料表面伤',type:'',value:'' },
                { name:'材料开裂',type:'',value:'' },
                { name:'棒料弯曲',type:'',value:'' },
                { name:'棒料大小不合格',type:'',value:'' },
                { name:'其它',type:'',value:'' }
            ],
            //装配车间 工废 料废
            zp_waste_gf:[
                { name:'打码',type:'',value:'' },
                { name:'压装',type:'',value:'' },
                { name:'焊接',type:'',value:'' },
                { name:'铆接',type:'',value:'' },
                { name:'调机',type:'',value:'' },
                { name:'返修',type:'',value:'' },
                { name:'外协',type:'',value:'' },
                { name:'其它',type:'',value:'' }
            ],
            zp_waste_lf:[
                { name:'大小边',type:'',value:'' },
                { name:'砂眼',type:'',value:'' },
                { name:'外协',type:'',value:'' },
                { name:'未光到',type:'',value:'' },
                { name:'裂纹',type:'',value:'' },
                { name:'卡滞',type:'',value:'' },
                { name:'尺寸',type:'',value:'' },
                { name:'其它',type:'',value:'' }
            ],
            dialogFormVisible:false,
            reasonList:[
                {label:"缺刀具",bindDataName:"toolShortage"},
                {label:"缺料",bindDataName:"materialShortage"},
                {label:"刀具异常",bindDataName:"toolAbnormalities"},
                {label:"计划停机",bindDataName:"waitingChangeover"},  //等待换型
                {label:"设备故障",bindDataName:"equipmentFailure"},
                {label:"产品换型",bindDataName:"productChangeover"},
                {label:"其它",bindDataName:"otherReason"}
            ],
            formData:{
                toolShortage:{id:"",downtimeType:"toolShortage",downtimeDuration:""},
                materialShortage:{id:"",downtimeType:"materialShortage",downtimeDuration:""},
                toolAbnormalities:{id:"",downtimeType:"toolAbnormalities",downtimeDuration:""},
                waitingChangeover:{id:"",downtimeType:"waitingChangeover",downtimeDuration:""},
                equipmentFailure:{id:"",downtimeType:"equipmentFailure",downtimeDuration:""},
                productChangeover:{id:"",downtimeType:"productChangeover",downtimeDuration:""},
                otherReason:{id:"",downtimeType:"otherReason",downtimeDuration:""}
            }
        }
    },
    computed:{
        scrapInfoList(){
            const type = type;
            const defineType = ['jjg_waste_gf','jjg_waste_lf','zxj_waste_gf','zxj_waste_lf','zp_waste_gf','zp_waste_lf'];
            const index = defineType.find(val => val == type);
            if(index !== -1){
                return this[type];
            } else {
                return [];
            }
        }
    },
    watch:{
        reasonDataList:{
            handler(newValue,oldValue){
                const tmp = newValue;
                const reasonList = this.reasonList;
                const formData = this.formData;
                if(tmp.length !== 0){
                    for(let i = 0; i < tmp.length; i ++){
                        for(let j = 0; j < reasonList.length; j ++){
                            if(tmp[i].downtimeType === reasonList[j].bindDataName){
                                formData[reasonList[j].bindDataName].downtimeDuration = tmp[i].downtimeDuration || "";
                                formData[reasonList[j].bindDataName].id = tmp[i].id || "";
                            }
                        }
                    }
                } else {
                    return;
                }
            },
            immediate:true
        }
    },
    methods:{
        validateInput(bindDataName) {
            const inputValue = this.formData[bindDataName].downtimeDuration;
            //const regex = /^\d+(\.\d{0,1})?$/;
            const regex = /^(?!00)\d+(\.\d{0,1})?$/;
            if (!regex.test(inputValue) && inputValue !== '') {
                this.formData[bindDataName].downtimeDuration = inputValue.slice(0, -1);
            }
            if(Number(inputValue) > 11){
                this.formData[bindDataName].downtimeDuration = inputValue.slice(0, -1);
            }
        },
        dealInputData(){
            const inputData = this.formData;
            const resultList = new Array();
            for(let key in inputData){
                inputData[key].downtimeDuration = inputData[key].downtimeDuration == "" ? "" : Number(inputData[key].downtimeDuration);
                resultList.push(inputData[key]);
            }
            return resultList;
        },
        clearBtnEvent(){
            const inputData = this.formData;
            const tmp = new Array();
            for(let key in inputData){
                tmp.push(key);
            }
            tmp.forEach(key => {
                inputData[key].downtimeDuration = "";
            })
        },
        confirmBtnEvent(){
            const tmp = this.dealInputData();
            const allHours = tmp.reduce((total,current,index,array) => {
                return total + Number(current.downtimeDuration);
            }, 0);
            if(allHours > 11){
                this.$message({type:"warning",message:"停机总时长已超过设备班次运行时长11小时"});
                return;
            }
            this.$emit("close",tmp);
            this.dialogFormVisible = false;
        },
        async dilogFormBeforeClose(done){
            const tmp = this.dealInputData();
            const allHours = tmp.reduce((total,current,index,array) => {
                return total + Number(current.downtimeDuration);
            }, 0);
            if(allHours > 11){
                this.$message({type:"warning",message:"停机总时长已超过设备班次运行时长11小时"});
                return;
            }
            this.$emit("close",tmp);
            done();
        }
    }
}
</script>

<style lang='scss' scoped>
.content{
    width: 100%;
    height: 100%;
}
</style>
<style lang="scss">
@media screen and (min-width:300px) and (max-width:600px) {
    .el-message-custom{
        width:320px;
    }
    .el-dialog{
        width:80% !important;
    }
}
</style>